<template>
  <view class="login-container">
    <uni-icons type="contact" size="100" color="#AFAFAF"></uni-icons>
    <button type="primary" class="btn-login" @tap="getToken" >一键登录</button>
    <text class="tips-text">登录后尽享更多权益</text>
  </view>
</template>

<script>
  // 按需导入 mapMutations 辅助函数
  import { mapMutations } from 'vuex'
  export default {
    name:"my-login",
    data() {
      return {
        
      };
    },
    methods: {
      ...mapMutations('m_user', ['updataUserInfo','updataToken']), 
      async getToken() {
       // 调用微信登录接口
      // const [err,res] = await uni.login().catch(err => err)
       uni.getUserProfile({
        desc:'登录',
        		success: async (resp)=>{
        			// 调用成功的话 准备参数对象
              uni.login({
                success: async (res) => {
                  console.log(res.code)
                  this.updataUserInfo(resp.userinfo)
                    console.log(resp)
                    const query = {
                      code: res.code,
                      encryptedData: resp.encryptedData,
                      iv: resp.iv,
                      rawData: resp.rawData,
                      signature: resp.signature
                    }
                    console.log(query)
                  const { data } = await uni.$http.post('/api/public/v1/users/wxlogin', query)
                  console.log(data)
                  if(data.meta.status === 400)  return uni.$showMsg('登录失败')
                  this.updataToken(data.message.token)
                }
              })
              },
        		fail:(err)=>{
        			uni.$showMsg('登录失败')
              console.log(err)
        		}

      })
      
      // console.log(res)
      // console.log(err)
      // 判断是否 uni.login调用失败
      // if (err || res.errMsg !== 'login:ok') return uni.$showError('登录失败')
      
      // 调用成功的话 准备参数对象
      // const query = { 
      //   code: res.code,
      //   encryptedData: info.encryptedData,
      //   iv: info.iv,
      //   rawData: info.rawData,
      //   signature: info.signature
      // }
     // const {data: loginResult} = await uni.$http.post('/api/public/v1/users/wxlogin', query)
     //  console.log(loginResult)
      
      // if(loginResult.meta.status !== 200) return uni.$showMsg('登录失败')
      
       // 2. 更新 vuex 中的 token
       // this.updataToken(loginResult.message.token)
      
      }
    }
  }
</script>

<style lang="scss">
.login-container {
  height: 750rpx;
  background-color: #f8f8f8;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  position: relative;
  
  &::after {
    content: ' ';
    display: block;
    width: 100%;
    height: 40px;
    background-color: white;
    border-radius: 100%;
    position: absolute;
    bottom: 0;
    left: 0;
    transform: translateY(50%);
    overflow: hidden;
  }
  
  .btn-login {
    width: 90%;
    border-radius: 100px;
    margin: 15px 0;
    background-color: #c00000;
  }
  .tips-text {
    font-size: 12px;
    color: gray;
  }
}
</style>